﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CNRegex.Web.Base;
using System.Configuration;
using CNRegex.Enum;
using CNRegex.Entity;
using CNRegex.Biz.BizAccess;
using CNRegex.Web.Utility;
using Project.Common;
using CNRegex.Biz;

namespace CNRegex.Web.Models
{
	public class ModelTutorialTemplate:BaseModel
	{
		
		protected RegexTutorial tutorial;
		protected BizRegexTutorial bizRegexTutorial;
		protected BizRegexTutorialCatalog bizRegexTutorialCatalog;
		protected BizRegexTutorialCatalogContent bizRegexTutorialCatalogContent;
		protected BizRegexTutorialCatalogContentComment bizRegexTutorialCatalogContentComment;

		protected Guid tutorialID;
		protected IList<RegexTutorialCatalog> tutorialCatalog;

		public string ExcuteControllerName { get; set; }
		public string ExcuteActionName { get; set; }

		public IList<RegexTutorialCatalog> TutorialCatalog
		{
			get { return tutorialCatalog; }
		}

		public RegexTutorial Tutorial
		{
			get
			{
				if (tutorial != null)
					return tutorial;
				tutorial = bizRegexTutorial.GetByID(tutorialID);
				return tutorial;
			}
		}

		public ModelTutorialTemplate(Guid tutorialID, string excuteControllerName, string tutorialActionName)
		{
			bizRegexTutorial=BizFactory.Get<BizRegexTutorial>();
			bizRegexTutorialCatalog=BizFactory.Get<BizRegexTutorialCatalog>();
			bizRegexTutorialCatalogContent = BizFactory.Get<BizRegexTutorialCatalogContent>();
			bizRegexTutorialCatalogContentComment = BizFactory.Get<BizRegexTutorialCatalogContentComment>();

			this.tutorialID = tutorialID;
			this.ExcuteActionName = tutorialActionName;
			this.ExcuteControllerName = excuteControllerName;
			tutorialCatalog = bizRegexTutorialCatalog.ListByTutorialID(tutorialID);
		}
	}


	public class ModelTutorialTemplateTutorialContent : ModelTutorialTemplate
	{
		private Guid currentCatalogID;

		public ModelTutorialTemplateTutorialContent(Guid currentCatalogID, Guid tutorialID,string executeControllerName,string tutorialActionName)
			: base(tutorialID, executeControllerName, tutorialActionName)
		{
			this.currentCatalogID = currentCatalogID;
		}

		public int? PageIndex { get; set; }
		public string CommentControllerName { get; set; }
		public string CommentActionName { get; set; }


		public Guid CurrentCatalogID
		{
			get { return currentCatalogID; }
			set { currentCatalogID = value; }
		}

		private RegexTutorialCatalog currentTutorialCatalog;
		public RegexTutorialCatalog CurrentTutorialCatalog
		{
			get
			{
				if (currentTutorialCatalog != null)
					return currentTutorialCatalog;
				currentTutorialCatalog = bizRegexTutorialCatalog.GetByID(currentCatalogID);
				return currentTutorialCatalog;
			}
		}

		private RegexTutorialCatalogContent currentTutorialCatalogContent;
		public RegexTutorialCatalogContent CurrentTutorialCatalogContent
		{
			get
			{
				if (currentTutorialCatalogContent != null)
					return currentTutorialCatalogContent;
				currentTutorialCatalogContent = bizRegexTutorialCatalogContent.ListBy(x => x.TutorialCatalogID == currentCatalogID, "", EnumOrder.ASC).FirstOrDefault();
				return currentTutorialCatalogContent;
			}
		}

		private PaginateHelper paginate;
		public PaginateHelper Paginate
		{
			get
			{
				currentTutorialCatalogContentComments = CurrentTutorialCatalogContentComments;
				return paginate;
			}
		}

		private IList<RegexTutorialCatalogContentCommentEx> currentTutorialCatalogContentComments;
		public IList<RegexTutorialCatalogContentCommentEx> CurrentTutorialCatalogContentComments
		{
			get
			{
				if (currentTutorialCatalogContentComments != null)
					return currentTutorialCatalogContentComments;
				int count = 0;
				currentTutorialCatalogContentComments = bizRegexTutorialCatalogContentComment.List(ProjectGlobal.PAGE_SIZE, PageIndex, CurrentTutorialCatalogContent.ID, ref count);
				paginate = new PaginateHelper(ProjectGlobal.PAGE_SIZE, count, PageIndex ?? 0, string.Format("/{1}/{2}/?id={0}&", CurrentTutorialCatalogContent.TutorialCatalogID, ExcuteControllerName, ExcuteActionName));
				paginate.EmptyTip = "暂无评论,简单讨论一下吧～～";
				return currentTutorialCatalogContentComments;
			}
		}

	}


}